package pcloud.client.scheduler;

import pcloud.client.util.FileResourceInfo;
import pcloud.client.util.Logger;
import pcloud.client.worker.*;

public class CloudEvent
{
	public enum Side
	{
		Client, Server
	};

	public enum Entity
	{
		Folder, File
	};

	public enum Action
	{
		Create, Modify, Rename, Delete
	};

	private Side side;
	private Entity entity;
	private Action action;
	private FileResourceInfo fileInfo;

//	public CloudEvent(Side side, Entity entity, Action action, String uniqueName)
//	{
//		this.side = side;
//		this.entity = entity;
//		this.action = action;
//		this.uniqueName = uniqueName;
//	}
	
	public CloudEvent(FileResourceInfo fileInfo, Action action)
	{
		this.side = fileInfo.isRemote()? Side.Server : Side.Client;
		this.entity = fileInfo.isFolder() ? Entity.Folder : Entity.File;
		this.action = action;
		this.fileInfo = fileInfo;
	}
	

	public CloudTask handle()
	{
		if (this.side.equals(Side.Client)) {
			if (this.entity.equals(Entity.File)) {
				if (this.action.equals(Action.Create)) {
					return new UploadTask(this.fileInfo);
				} else if (this.action.equals(Action.Modify)) {
					return new UploadTask(this.fileInfo);
				} else if (this.action.equals(Action.Delete)) {
					return new RemoteDeleteTask(this.fileInfo.toRemote());
				}
			} else if (this.entity.equals(Entity.Folder)) {
				if (this.action.equals(Action.Create)) {
					return new RemoteCreateTask(this.fileInfo.toRemote());
				} else if (this.action.equals(Action.Modify)) {
					//--The only way to modify a folder, is change files in it, so don't do anything here.
					return new EmptyTask(this.fileInfo);
				} else if (this.action.equals(Action.Delete)) {
					return new RemoteDeleteTask(this.fileInfo.toRemote());
				}
			}
		} else if (this.side.equals(Side.Server)) {
			if (this.entity.equals(Entity.File)) {
				if (this.action.equals(Action.Create)) {
					return new DownloadTask(this.fileInfo);
				} else if (this.action.equals(Action.Modify)) {
					return new DownloadTask(this.fileInfo);
				} else if (this.action.equals(Action.Delete)) {
					return new LocalDeleteTask(this.fileInfo);
				}
			} else if (this.entity.equals(Entity.Folder)) {
				if (this.action.equals(Action.Create)) {
					return new LocalCreateTask(this.fileInfo);
				} else if (this.action.equals(Action.Modify)) {
					//--The only way to modify a folder, is change files in it, so don't do anything here.
					return new EmptyTask(this.fileInfo);
				} else if (this.action.equals(Action.Delete)) {
					return new LocalDeleteTask(this.fileInfo);
				}
			}
		}
		logger.info("Strange Event: "+this, "Error");
		return (CloudTask) null;
	}
	
	public String toString()
	{
		return "{FileInfo: "+fileInfo.toString()+" Side:"+side+" Entity:"+entity+", Action: "+ action+ "}";
	}
	
}